﻿using QLKetNoi;
using System;
using System.Collections.Generic;
using System.Data.Linq;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace QLDVu.BO
{
    public class PhieuThuTienBO
    {
        public static Table<NGUOIDUNG> DanhSachNguoiDung()
        {
            QLDNDataContext dulieu = new QLDNDataContext();
            return dulieu.GetTable<NGUOIDUNG>();


        }
        public static Table<PHIEUTHU> DanhSachPhieuThu()
        {
            QLDNDataContext dulieu = new QLDNDataContext();
            return dulieu.GetTable<PHIEUTHU>();

        }
        public static Table<SUACHUA> DanhSachPhieuSuaChua()
        {
            QLDNDataContext dulieu = new QLDNDataContext();
            return dulieu.GetTable<SUACHUA>();

        }
        public static PHIEUTHU DanhSachPhieuThu(string maphieuthu)
        {
            QLDNDataContext dulieu = new QLDNDataContext();
            Table<PHIEUTHU> phieuthu = dulieu.GetTable<PHIEUTHU>();
            PHIEUTHU x = phieuthu.Single(kh => kh.MaPhieuThuTien.Equals(maphieuthu));
            return x;

        }
        public static SUACHUA PhieuSuaChuaTheoMa(string masuachua)
        {



            QLDNDataContext dulieu = new QLDNDataContext();
            Table<SUACHUA> phieusuachua = dulieu.GetTable<SUACHUA>();
            SUACHUA x = phieusuachua.Single(xx => xx.MaSuaChua.Equals(masuachua));
            return x;
        }
        public static BAOGIA PhieuBaoGiaTheoMa(string mabaogia)
        {



            QLDNDataContext dulieu = new QLDNDataContext();
            Table<BAOGIA> phieusuachua = dulieu.GetTable<BAOGIA>();
            BAOGIA x = phieusuachua.Single(xx => xx.MaBaoGia.Equals(mabaogia));
            return x;
        }


        public static PHIEUTHU MaPhieuThuCuoiCung()
        {
            QLDNDataContext dulieu = new QLDNDataContext();
            PHIEUTHU s = dulieu.PHIEUTHUs.OrderByDescending(hh => hh.MaPhieuThuTien).FirstOrDefault();
            return s;
        }


        public static void ThemPhieuThuTien(string maphieuthutien, string manhanvien, string nguoinoptien, string maphieusuachua, string ngaythutien, string tienthu, string tienconlai,string dienthoai,string diachi)
        {
            QLDNDataContext dulieu = new QLDNDataContext();
            Table<PHIEUTHU> phieuthu = dulieu.GetTable<PHIEUTHU>();
            PHIEUTHU x = new PHIEUTHU();
            x.MaPhieuThuTien = maphieuthutien;
            x.MaNhanVien = manhanvien;
            x.NguoiNopTien = nguoinoptien;
            x.MaPhieuSuaChua = maphieusuachua;
            x.NgayThuTien = Convert.ToDateTime(ngaythutien);
            x.TienThu = Convert.ToInt32(tienthu);
            x.TienConLai = Convert.ToInt32(tienconlai);
            x.DiaChi = diachi;
            x.DienThoai = dienthoai;
            phieuthu.InsertOnSubmit(x);
            dulieu.SubmitChanges();

        }
        public static void CapNhatPhieuSuaChua(string maphieusuachua, string conlai)
        {
            QLDNDataContext dulieu = new QLDNDataContext();
            Table<SUACHUA> phieusuachua = dulieu.GetTable<SUACHUA>();
            SUACHUA pbg = phieusuachua.Single(psc => psc.MaSuaChua.Equals(maphieusuachua));
            pbg.ConLai = Convert.ToInt32(conlai);
            dulieu.SubmitChanges();
        }
        public static void CapNhatPhieuThuTien(string maphieuthutien,string manhanvien,string nguoinoptien,string maphieusuachua,string ngaythutien,string tienthu,string tienconlai,string diachi,string dienthoai)
        {
            QLDNDataContext dulieu = new QLDNDataContext();
            Table<PHIEUTHU> phieuthu = dulieu.GetTable<PHIEUTHU>();
            PHIEUTHU pbg = phieuthu.Single(psc => psc.MaPhieuThuTien.Equals(maphieuthutien));
            pbg.MaPhieuThuTien= maphieuthutien;
            pbg.MaNhanVien = manhanvien;
            pbg.MaPhieuSuaChua = maphieusuachua;
            pbg.NguoiNopTien = nguoinoptien;
            pbg.NgayThuTien =Convert.ToDateTime(ngaythutien);
            pbg.TienThu =Convert.ToInt32(tienthu);
            pbg.TienConLai = Convert.ToInt32(tienconlai);
            pbg.DiaChi = diachi;
            pbg.DienThoai = dienthoai;
            dulieu.SubmitChanges();
        }
        public static void XoaPhieuThuTien(string maphieuthutien)
        {
            QLDNDataContext dulieu = new QLDNDataContext();
            Table<PHIEUTHU> chitietbaocaono = dulieu.GetTable<PHIEUTHU>();
            var dschitietbcn = from ct in chitietbaocaono
                               where ct.MaPhieuThuTien.Equals(maphieuthutien)
                               select ct;
            chitietbaocaono.DeleteAllOnSubmit(dschitietbcn);
            dulieu.SubmitChanges();
        }

    }
}
